package zOthers;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;

/**
 * This class aims to reduce the excessive cost of image IO by
 * loading all the images for only one time right at the beginning of launching;
 * also, it provides accessing methods to those buffered images for others.
 */
public class ImageLoader {

	private final ArrayList<ImageStructure> imagesList;

	/*
	 * Constructor
	 */
	public ImageLoader() {
		imagesList = new ArrayList<ImageStructure>();

		/*
		 * Load Images of menu
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_PANEL_LOGIN_BACKGROUND, loadImage(ImageStructure.IMAGE_NAME_PANEL_LOGIN_BACKGROUND)));

		/*
		 * Load images of blocks
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BASE, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BASE)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_LEFT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_RIGHT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_LEFT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_RIGHT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_FIELD_1, loadImage(ImageStructure.IMAGE_NAME_BLOCK_FIELD_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_FIELD_2, loadImage(ImageStructure.IMAGE_NAME_BLOCK_FIELD_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_WATER_1, loadImage(ImageStructure.IMAGE_NAME_BLOCK_WATER_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_WATER_2, loadImage(ImageStructure.IMAGE_NAME_BLOCK_WATER_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_WATER_3, loadImage(ImageStructure.IMAGE_NAME_BLOCK_WATER_3)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_MOUNTAIN, loadImage(ImageStructure.IMAGE_NAME_BLOCK_MOUNTAIN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_FOREST, loadImage(ImageStructure.IMAGE_NAME_BLOCK_FOREST)));

		/*
		 * Load images of units
		 */

		/*
		 * Load images of cursors
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_CURSOR_LEFT, loadImage(ImageStructure.IMAGE_NAME_CURSOR_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_CURSOR_RIGHT, loadImage(ImageStructure.IMAGE_NAME_CURSOR_RIGHT)));
	}

	private BufferedImage loadImage(String imagePath) {
		BufferedImage image = null;
		try {
			image = ImageIO.read(new File(imagePath));
		} catch(Exception ex) {
			System.out.println("Fail to load image... " + imagePath);
		}
		return image;
	}

	/*
	 * Accessors
	 */
	public BufferedImage getImageByName(String imageName) {
		for(int i = 0; i < imagesList.size(); i++) {
			if(imagesList.get(i).getImageName().equals(imageName))
				return imagesList.get(i).getImage();
		}
		return null;
	}
}
